package com.example.user.login

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Intent
import android.os.Build
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_base.mvvm.BaseActivity
import com.example.lib_base.utils.EventBus
import com.example.user.R
import com.example.user.databinding.ActivityLoginCodeBinding
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@Route(path = "/user/LoginCodeActivity")
class LoginCodeActivity : BaseActivity<ActivityLoginCodeBinding,LoginViewModel>() {
    override fun initData() {
        //发送成功 //按钮不可用 30秒后可再次使用 发送一个通知
        viewModel.sendCodeSuccess.observe(this){
            if(it.code==200){
                binding.btnGetcode.isEnabled = false
                //发送通知
                val manager =
                    getSystemService(NOTIFICATION_SERVICE) as NotificationManager
                if(Build.VERSION.SDK_INT>Build.VERSION_CODES.O){
                    //判断版本---高版本
                    val notificationChannel =
                        NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT)
                    manager.createNotificationChannel(notificationChannel)
                    //通知对象
                    val intent = Intent(this,LoginCodeActivity::class.java)
                    val pendingIntent = PendingIntent.getActivity(this,102,intent,PendingIntent.FLAG_UPDATE_CURRENT)
                    val notification = Notification.Builder(this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("验证码通知")
                        .setContentText("短信验证码:${it.data.content.substring(8, 14)}")
                        .setChannelId("id")
                        .setContentIntent(pendingIntent)//当app退出时，点击通知 能够将app打开
                        .setAutoCancel(true)
                        .build()
                    //发送通知
                    manager.notify(1,notification)

                }else{
                    val intent = Intent(this,LoginCodeActivity::class.java)
                    val pendingIntent = PendingIntent.getActivity(this,102,intent,PendingIntent.FLAG_UPDATE_CURRENT)
                    val notification = Notification.Builder(this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("验证码通知")
                        .setContentText("短信验证码:${it.data.content.substring(8, 14)}")
                        .setContentIntent(pendingIntent)//当app退出时，点击通知 能够将app打开
                        .setAutoCancel(true)
                        .build()
                    //发送通知
                    manager.notify(1,notification)
                }

                lifecycleScope.launch {
                    for (i in 30 downTo 1){
                        binding.btnGetcode.text = "${i}秒后获取"
                        delay(1000)
                    }
                    binding.btnGetcode.isEnabled = true
                    binding.btnGetcode.text = "获取验证码"
                }
            }
        }

        viewModel.loginCodeSuccess.observe(this){
            ToastUtils.showLong(it.data.toString())
        }
        EventBus.instance.getEvent<String>("tvGoLogin").observe(this){
            ToastUtils.showLong("点击了密码登录，想要跳转的界面路径为$it")
            //ARouter.getInstance().build(it).navigation()
        }
    }

    override fun initView() {
        //点击 获取验证码  实现发送验证码
        binding.btnGetcode.setOnClickListener {
            val etUsername = binding.etUsername.text.toString()
            if(etUsername.isEmpty()){
                ToastUtils.showLong("手机号不能为空")
                return@setOnClickListener
            }
            if(!RegexUtils.isMobileExact(etUsername)){
                ToastUtils.showLong("手机号格式不正确，请检查")
                return@setOnClickListener
            }
            viewModel.sendCode(etUsername)
        }
        //点击登录 调用验证码登录
        binding.btnLogincode.setOnClickListener {
            val etUsername = binding.etUsername.text.toString()
            if(etUsername.isEmpty()){
                ToastUtils.showLong("手机号不能为空")
                return@setOnClickListener
            }
            if(!RegexUtils.isMobileExact(etUsername)){
                ToastUtils.showLong("手机号格式不正确，请检查")
                return@setOnClickListener
            }
            val etCode = binding.etCode.text.toString()
            if(etCode.isEmpty()){
                ToastUtils.showLong("验证码不能为空")
                return@setOnClickListener
            }
            viewModel.loginCode(etCode,etUsername)
        }

        //点击密码登录文本，使用事件总线 实现调用
        binding.tvGologin.setOnClickListener {
            EventBus.instance.postEvent("tvGoLogin","/user/LoginActivty")
        }
    }

    override fun getViewModel(): Class<LoginViewModel>  = LoginViewModel::class.java

    override fun getLayout(): Int  = R.layout.activity_login_code

}